package com.example.campusforumapi.service.impl;

import com.example.campusforumapi.dao.ActivityMapper;
import com.example.campusforumapi.dao.ArticleMapper;
import com.example.campusforumapi.entity.*;
import com.example.campusforumapi.service.ActivityService;
import com.example.campusforumapi.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public List<ActivityTag> getCategories() {
        return activityMapper.getCategories();
    }

    @Override
    public List<Department> getDepartments() {
        return activityMapper.getDepartments();
    }
    /*
     * 活动分页查询
     * */
    @Override
    public PageResult getActivityPage(Integer page, Integer pageSize, Integer activityId, Integer userId, String title, Integer tagId, Integer departmentId,Integer status, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Activity> activityList = activityMapper.list(activityId,userId,title,tagId,departmentId,status,startTime,endTime);
        Page<Activity> p = (Page<Activity>) activityList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public int delActivity(List<Integer> ids) {
        return activityMapper.delActivity(ids);
    }
    /*
     * 添加
     * @paramActivity
     * */
    @Override
    public int insertActivity(Activity activity) {
        if(activity.getStatus()==0){
            activity.setSubmitTime(LocalDateTime.now());
            activity.setUpdateTime(LocalDateTime.now());
        }else if(activity.getStatus()==1) {
            activity.setPublishTime(LocalDateTime.now());
            activity.setUpdateTime(LocalDateTime.now());
        }
        else{
            activity.setUpdateTime(LocalDateTime.now());
        }
        return activityMapper.insertActivity(activity);
    }

    @Override
    public int updActivity(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());//更新修改时间
        if(activity.getStatus()==0){
            activity.setSubmitTime(LocalDateTime.now());
        }else if(activity.getStatus()>=1 && activity.getStatus()<5 && activity.getPublishTime()==null) {
            activity.setPublishTime(LocalDateTime.now());
        }else if(activity.getStatus()==5){
            activity.setPublishTime(null);
        }
        return activityMapper.updActivity(activity);
    }

    @Override
    public PageResult getAuditActivityPage(Integer page, Integer pageSize, Integer activityId) {
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Activity> activityAuditList = activityMapper.AuditList(activityId);
        Page<Activity> p = (Page<Activity>) activityAuditList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    //修改活动审核状态
    @Override
    public int updAproveActivity(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());//更新修改时间
        activity.setPublishTime(LocalDateTime.now());//更新发布时间
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 获取活动的开始时间和结束时间
        LocalDateTime startTime = activity.getStartTime();
        LocalDateTime endTime = activity.getEndTime();

        // 根据当前时间与活动的开始时间和结束时间的比较，设置活动状态
        if (now.isBefore(startTime)) {
            // 如果当前时间在活动开始时间之前，设置状态为1（未开始）
            activity.setStatus(1);
        } else if (now.isAfter(startTime) && now.isBefore(endTime)) {
            // 如果当前时间在活动开始时间和结束时间之间，设置状态为2（进行中）
            activity.setStatus(2);
        } else {
            // 如果当前时间在活动结束时间之后，设置状态为3（已结束）
            activity.setStatus(3);
        }
        return activityMapper.updAproveActivity(activity);
    }

    @Override
    public int updRejectActivity(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());//更新修改时间
        activity.setStatus(5);
        return activityMapper.updRejectActivity(activity);
    }

}
